// json_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "JSONParse.h"

#if 1
int main()
{
	cJSON *JsonObject1 = cJSON_CreateObject();
	cJSON_AddItemToObject(JsonObject1, "code", cJSON_CreateNumber(200));
	cJSON_AddItemToObject(JsonObject1, "res", cJSON_CreateString("success"));

	cJSON *JsonObject2 = cJSON_CreateObject();
	cJSON_AddItemToObject(JsonObject1, "msg", JsonObject2);
	cJSON_AddItemToObject(JsonObject2, "meetid", cJSON_CreateString("13603611579"));
	cJSON_AddItemToObject(JsonObject2, "Speaker", cJSON_CreateString("101"));
	cJSON_AddItemToObject(JsonObject2, "MeetType", cJSON_CreateNumber(0));
	cJSON_AddItemToObject(JsonObject2, "EndTime", cJSON_CreateString("1"));

	cJSON *JsonArray3 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject2, "Member", JsonArray3);
	cJSON_AddItemToObject(JsonObject2, "Theme", cJSON_CreateString("ABCD"));

	cJSON *JsonObject4 = cJSON_CreateObject();
	cJSON_AddItemToArray(JsonArray3, JsonObject4);

	cJSON_AddItemToObject(JsonObject4, "account_1", cJSON_CreateString("136036101"));
	cJSON_AddItemToObject(JsonObject4, "name", cJSON_CreateString("JW"));

	cJSON *JsonArray5 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4, "ip", JsonArray5);
	cJSON_AddItemToArray(JsonArray5, cJSON_CreateString("192.168.19.1"));
	cJSON_AddItemToArray(JsonArray5, cJSON_CreateString("172.16.1.8"));

	cJSON *JsonArray6 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4, "flow", JsonArray6);
	cJSON_AddItemToArray(JsonArray6, cJSON_CreateString("12"));
	cJSON_AddItemToArray(JsonArray6, cJSON_CreateString("23"));

	cJSON_AddItemToObject(JsonObject4, "speech", cJSON_CreateNumber(1));

	cJSON *JsonObject4_1 = cJSON_CreateObject();
	cJSON_AddItemToArray(JsonArray3, JsonObject4_1);
	cJSON_AddItemToObject(JsonObject4_1, "account_1", cJSON_CreateString("136036101_1"));
	cJSON_AddItemToObject(JsonObject4_1, "name", cJSON_CreateString("JW_1"));

	cJSON *JsonArray5_1 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4_1, "ip", JsonArray5_1);
	cJSON_AddItemToArray(JsonArray5_1, cJSON_CreateString("192.168.19.1_1"));
	cJSON_AddItemToArray(JsonArray5_1, cJSON_CreateString("172.16.1.8_1"));

	cJSON *JsonArray6_1 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4_1, "flow", JsonArray6_1);
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateString("12_1"));
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateString("23_1"));
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateNumber(1));
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateNumber(2));

	cJSON_AddItemToObject(JsonObject4_1, "speech", cJSON_CreateNumber(2));

	char* SendJson1 = cJSON_PrintUnformatted(JsonObject1);
	printf("%d\n", strlen(SendJson1));
	printf("%s\n", SendJson1);

	char *SendJson = cJSON_Print(JsonObject1);
	printf("%d\n", strlen(SendJson));
	printf("%s\n", SendJson);
	//	if (JsonObject1) { cJSON_Delete(JsonObject1); JsonObject1 = NULL; }

	CJSONParse parse(SendJson);
	int nCode = 0;
	std::string strRes = "";
	parse.GetValueNumber(parse.GetRootItem(), "code", nCode);
	printf("code:%d\n", nCode);
	parse.GetValueStr(parse.GetRootItem(), "res", strRes);
	printf("res:%s\n", strRes.c_str());

	cJSON* Jsonmsg = parse.GetFirstItem("msg");
	if (!Jsonmsg)return 0;
	std::string strMeetid = "";
	std::string strSpeaker = "";
	std::string strTheme = "";
	int nMeetType = 0;
	std::string strEndTime = "";
	std::string strMember = "";
	parse.GetValueStr(Jsonmsg, "meetid", strMeetid);
	printf("meetid:%s\n", strMeetid.c_str());
	parse.GetValueStr(Jsonmsg, "Speaker", strSpeaker);
	printf("Speaker:%s\n", strSpeaker.c_str());
	parse.GetValueStr(Jsonmsg, "Theme", strTheme);
	printf("Theme:%s\n", strTheme.c_str());
	parse.GetValueNumber(Jsonmsg, "MeetType", nMeetType);
	printf("MeetType:%d\n", nMeetType);
	parse.GetValueStr(Jsonmsg, "EndTime", strEndTime);
	printf("EndTime:%s\n", strEndTime.c_str());

	cJSON* JsonMember = parse.GetFirstItem("msg/Member");
	int JsonIp_array_size = cJSON_GetArraySize(JsonMember);
	int i = 0;
	cJSON *JsonIp_array_item;
	for (i = 0; i < JsonIp_array_size; i++) {
		JsonIp_array_item = cJSON_GetArrayItem(JsonMember, i);
		std::string strAccount = "";
		std::string strName = "";
		std::string strIp = "";
		if (!JsonIp_array_item)return 0;
		parse.GetValueStr(JsonIp_array_item, "account_1", strAccount);
		printf("account_1:%s\n", strAccount.c_str());
		parse.GetValueStr(JsonIp_array_item, "name", strName);
		printf("name:%s\n", strName.c_str());

		cJSON* JsonIp = cJSON_GetObjectItem(JsonIp_array_item, "ip");
		if (JsonIp)
		{
			int JsonIp_array_size = cJSON_GetArraySize(JsonIp);
			int i = 0;
			cJSON *JsonIp_item;
			for (i = 0; i < JsonIp_array_size; i++) {
				JsonIp_item = cJSON_GetArrayItem(JsonIp, i);
				printf("ip:%s\n", JsonIp_item->valuestring);
			}

			cJSON* JsonFlow = cJSON_GetObjectItem(JsonIp_array_item, "flow");
			int JsonFlow_array_size = cJSON_GetArraySize(JsonIp);
			int j = 0;
			cJSON *JsonFlow_item;
			for (j = 0; j < JsonFlow_array_size; j++) {
				JsonFlow_item = cJSON_GetArrayItem(JsonFlow, j);
				printf("flow:%s\n", JsonFlow_item->valuestring);
			}
		}
		int nSpeech = 0;
		parse.GetValueNumber(JsonIp_array_item, "speech", nSpeech);
		printf("speech:%d\n", nSpeech);
	}

	system("pause");
	return 0;
}
#else

int main()
{
	cJSON *JsonObject1 = cJSON_CreateObject();
	cJSON_AddItemToObject(JsonObject1, "code", cJSON_CreateNumber(200));
	cJSON_AddItemToObject(JsonObject1, "res", cJSON_CreateString("success"));

	cJSON *JsonObject2 = cJSON_CreateObject();
	cJSON_AddItemToObject(JsonObject1, "msg", JsonObject2);
	cJSON_AddItemToObject(JsonObject2, "meetid", cJSON_CreateString("13603611579"));
	cJSON_AddItemToObject(JsonObject2, "Speaker", cJSON_CreateString("101"));
	cJSON_AddItemToObject(JsonObject2, "MeetType", cJSON_CreateNumber(0));
	cJSON_AddItemToObject(JsonObject2, "EndTime", cJSON_CreateString("1"));

	cJSON *JsonArray3 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject2, "Member", JsonArray3);
	cJSON_AddItemToObject(JsonObject2, "Theme", cJSON_CreateString("这个中国人"));

	cJSON *JsonObject4 = cJSON_CreateObject();
	cJSON_AddItemToArray(JsonArray3, JsonObject4);
	
	cJSON_AddItemToObject(JsonObject4, "account", cJSON_CreateString("136036101"));
	cJSON_AddItemToObject(JsonObject4, "name", cJSON_CreateString("JW"));

	cJSON *JsonArray5 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4, "ip", JsonArray5);
	cJSON_AddItemToArray(JsonArray5, cJSON_CreateString("192.168.19.1"));
	cJSON_AddItemToArray(JsonArray5, cJSON_CreateString("172.16.1.8"));

	cJSON *JsonArray6 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4, "flow", JsonArray6);
	cJSON_AddItemToArray(JsonArray6, cJSON_CreateString("12"));
	cJSON_AddItemToArray(JsonArray6, cJSON_CreateString("23"));

	cJSON_AddItemToObject(JsonObject4, "speech", cJSON_CreateNumber(1));

	cJSON *JsonObject4_1 = cJSON_CreateObject();
	cJSON_AddItemToArray(JsonArray3, JsonObject4_1);
	cJSON_AddItemToObject(JsonObject4_1, "account_1", cJSON_CreateString("136036101_1"));
	cJSON_AddItemToObject(JsonObject4_1, "name", cJSON_CreateString("JW_1"));

	cJSON *JsonArray5_1 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4_1, "ip", JsonArray5_1);
	cJSON_AddItemToArray(JsonArray5_1, cJSON_CreateString("192.168.19.1_1"));
	cJSON_AddItemToArray(JsonArray5_1, cJSON_CreateString("172.16.1.8_1"));

	cJSON *JsonArray6_1 = cJSON_CreateArray();
	cJSON_AddItemToObject(JsonObject4_1, "flow", JsonArray6_1);
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateString("12_1"));
	cJSON_AddItemToArray(JsonArray6_1, cJSON_CreateString("23_1"));

	cJSON_AddItemToObject(JsonObject4_1, "speech", cJSON_CreateNumber(2));

	char *SendJson = cJSON_Print(JsonObject1);
	printf("%s\n", SendJson);
	//	if (JsonObject1) { cJSON_Delete(JsonObject1); JsonObject1 = NULL; }

	CJSONParse parse(SendJson);
	int nCode = 0;
	std::string strRes = "";
	parse.GetValueNumber(parse.GetRootItem(), "code", nCode);
	printf("code:%d\n", nCode);
	parse.GetValueStr(parse.GetRootItem(), "res", strRes);
	printf("res:%s\n", strRes.c_str());

	cJSON* Jsonmsg = parse.GetFirstItem("msg");
	if (!Jsonmsg)return 0;
	std::string strMeetid = "";
	std::string strSpeaker = "";
	std::string strTheme = "";
	int nMeetType = 0;
	std::string strEndTime = "";
	std::string strMember = "";
	parse.GetValueStr(Jsonmsg, "meetid", strMeetid);
	printf("meetid:%s\n", strMeetid.c_str());
	parse.GetValueStr(Jsonmsg, "Speaker", strSpeaker);
	printf("Speaker:%s\n", strSpeaker.c_str());
	parse.GetValueStr(Jsonmsg, "Theme", strTheme);
	printf("Theme:%s\n", strTheme.c_str());
	parse.GetValueNumber(Jsonmsg, "MeetType", nMeetType);
	printf("MeetType:%d\n", nMeetType);
	parse.GetValueStr(Jsonmsg, "EndTime", strEndTime);
	printf("EndTime:%s\n", strEndTime.c_str());

	cJSON* JsonMember = parse.GetFirstItem("msg/Member");
	int JsonIp_array_size = cJSON_GetArraySize(JsonMember);
	int i = 0;
	cJSON *JsonIp_array_item;
	for (i = 0; i < JsonIp_array_size; i++) {
		JsonIp_array_item = cJSON_GetArrayItem(JsonMember, i);
		std::string strAccount = "";
		std::string strName = "";
		std::string strIp = "";
		if (!JsonIp_array_item)return 0;
		parse.GetValueStr(JsonIp_array_item, "account_1", strAccount);
		printf("account_1:%s\n", strAccount.c_str());
		parse.GetValueStr(JsonIp_array_item, "name", strName);
		printf("name:%s\n", strName.c_str());

		cJSON* JsonIp = cJSON_GetObjectItem(JsonIp_array_item, "ip");
		if (JsonIp)
		{
			int JsonIp_array_size = cJSON_GetArraySize(JsonIp);
			int i = 0;
			cJSON *JsonIp_item;
			for (i = 0; i < JsonIp_array_size; i++) {
				JsonIp_item = cJSON_GetArrayItem(JsonIp, i);
				printf("ip:%s\n", JsonIp_item->valuestring);
			}

			cJSON* JsonFlow = cJSON_GetObjectItem(JsonIp_array_item, "flow");
			int JsonFlow_array_size = cJSON_GetArraySize(JsonIp);
			int j = 0;
			cJSON *JsonFlow_item;
			for (j = 0; j < JsonFlow_array_size; j++) {
				JsonFlow_item = cJSON_GetArrayItem(JsonFlow, j);
				printf("flow:%s\n", JsonFlow_item->valuestring);
			}
		}
		int nSpeech = 0;
		parse.GetValueNumber(JsonIp_array_item, "speech", nSpeech);
		printf("speech:%d\n", nSpeech);
	}
	
	system("pause");
	return 0;
}
#endif